/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servidor;

import java.util.ArrayList;

/**
 *
 * @author Kaotar
 */
public class Channel {

    private ArrayList<Servidor> users = new ArrayList();
    private String name;
    private boolean isCreateFromServer;
    private ServidorMultThread servidor;
    
    public Channel(String name, boolean servIs, ServidorMultThread serv) {
        this.name = name;
        this.servidor = serv;
        this.isCreateFromServer = servIs;
    }
    
    public ArrayList<Servidor> getUsers(){
        return users;
    }

    public void sendMessage(String msg, Servidor fromNick) {
        for (Servidor i : users) {
            i.sendMessage(msg, fromNick);
        }
    }

    public void leaveChannel(Servidor user) {
        int index = users.indexOf(user);
        if (index != -1) {
            users.remove(index);
        }
        if (isCreateFromServer == false && users.size() == 0) {
            servidor.deleteChannel(this);
        }
    }
    
    private boolean isAlreadyJoinned(Servidor user) {
         int index = users.indexOf(user);
         if (index == -1) {
             return false;
         }
         return true;
    }
    
    public int joinChannel(Servidor user) {
        if (isAlreadyJoinned(user) == false) {
            users.add(user);
            return 0;
        } else { 
            return -1;
        }
    }
    
    public String getName() {
        return name;
    }
}
